package org.mtmi.midi;

import org.mtmi.ui.M;
import org.mtmi.ui.controls.ToggleButton;
import org.mtmi.ui.controls.listeners.ToggleEvent;
import org.mtmi.ui.controls.listeners.ToggleTapListener;
import org.mtmi.ui.scenes.Scene;

import rwmidi.MidiOutput;

public class SendMidiCCButton extends ToggleButton implements ToggleTapListener {

	int midiChannel;
	int ccNumber;
	int ccMinValue;
	int ccMaxValue;
	MidiOutput midiOutput;
	

	public SendMidiCCButton(Scene scene) {
		super(scene, M.SCENE_CONTROL);
	}


	public SendMidiCCButton(Scene scene, int midiChannel, int ccNumber,
			int ccMinvalue, int ccMaxValue) {
		super(scene, M.SCENE_CONTROL);
		addToggleTapListener(this);
		this.midiChannel = midiChannel;
		this.ccNumber = ccNumber;
		this.ccMinValue = ccMinvalue;
		this.ccMaxValue = ccMaxValue;
	}

	
	public MidiOutput getMidiOutput() {
		return midiOutput;
	}
	
	public void setMidiOutput(MidiOutput midiOutput) {
		this.midiOutput = midiOutput;
	}
	
	public int getMidiChannel() {
		return midiChannel;
	}
	
	public void setMidiChannel(int midiChannel) {
		this.midiChannel = midiChannel;
	}

	
	public int getCCNumber() {
		return ccNumber;
	}
	
	public void setCCNumber(int ccNumber) {
		this.ccNumber = ccNumber;
	}

	
	public int getCCMinValue() {
		return ccMinValue;
	}
	
	public void setCCMinValue(int ccMinvalue) {
		this.ccMinValue = ccMinvalue;
	}

	
	public int getCCMaxValue() {
		return ccMaxValue;
	}
	
	public void setCCMaxValue(int setCCMaxValue) {
		this.ccMaxValue = setCCMaxValue;
	}

	private void sendCCMessage(int ccValue) {
		// Debug
		System.out.println("sending Control Change " + ccNumber + "on channel "
				+ midiChannel + " with value " + ccValue);
		midiOutput.sendController(midiChannel, ccNumber,
				ccValue);
	}


	@Override
	public void toggleTapped(ToggleEvent e) {
		if (isTapped) {
			sendCCMessage(ccMaxValue);
		} else {
			sendCCMessage(ccMinValue);
		}
	}

}
